// 删除评论
import {User} from "~/server/models/user";

export default defineEventHandler(async (event) => {
    try {
        const method = event.node.req.method;

        if (method !== 'DELETE') {
            const errorMessage = `不支持的方法: ${method}`;
            console.error(errorMessage);
            throw new Error(errorMessage); // 抛出错误，因为这不是预期的DELETE请求
        }
        const query = getQuery(event);
        if (!query._id) {
            throw new Error("缺少参数_id");
        }
        const id = query._id;

        const deletedComment = await User.deleteOne({_id: id});
        if (!deletedComment) {
            throw new Error("删除失败, 原因是未找到");
        }

        return {message: "删除成功"};
    } catch (error) {
        console.error("操作过程中发生错误:", error);
        return {
            error: "操作过程中发生错误",
            details: error instanceof Error ? error.message : "未知错误",
        };
    }
});
